package vip.specimen.system.controller.question;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import vip.specimen.system.common.annotation.SystemJurisdiction;
import vip.specimen.system.common.enums.QuestionTypeEnum;
import vip.specimen.system.common.enums.ResultCode;
import vip.specimen.system.datasource.entity.question.QuestionProblemEntity;
import vip.specimen.system.datasource.validation.question.QuestionProblemValidation;
import vip.specimen.system.result.JsonResult;
import vip.specimen.system.service.question.ProblemService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/4/27 14:43
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Api(tags = "问题在线管理")
@RestController
@RequestMapping(value = "/question/question")
public class QuestionController {
    @Resource
    private ProblemService problemService;

    @GetMapping(value = "type")
    @ApiOperation(value = "查看问题类型", notes = "获取全部的问题类型方式")
    @RequiresPermissions(value = "question:question:select:questionType")
    public JsonResult<ArrayList<HashMap<String, Object>>> getSaveMethod() {
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        for (QuestionTypeEnum type : QuestionTypeEnum.values()) {
            HashMap<String, Object> map = new HashMap<>(3);
            map.put("typeId", type.getTypeId());
            map.put("typeName", type.getTypeName());
            map.put("name", type.name());
            list.add(map);
        }
        return JsonResult.success(list);
    }

    @PutMapping
    @RequiresPermissions("question:question:insert")
    @ApiOperation(value = "新增", notes = "新增问题")
    @SystemJurisdiction(resourceType = "button", permissionName = "添加问题")
    public JsonResult<QuestionProblemEntity> insert(@RequestBody @Valid QuestionProblemValidation questionProblemValidation) {
        return JsonResult.success(this.problemService.insert(questionProblemValidation));
    }

    @GetMapping
    @RequiresPermissions(value = "question:question:select")
    @ApiOperation(value = "查看", notes = "获取问题列表")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看问题列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", dataType = "int")
    })
    public JsonResult<Page<QuestionProblemEntity>> articlePageAll() {
        return JsonResult.success(this.problemService.findAllPage());
    }


    @PostMapping(value = "enable")
    @RequiresPermissions(value = "question:question:update:enable")
    @ApiOperation(value = "更新状态", notes = "根据uuid更新问题状态")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新问题状态")
    public JsonResult<QuestionProblemEntity> update(@RequestParam("uuid") UUID uuid) {
        QuestionProblemEntity result = this.problemService.update(uuid);
        return null == result ? JsonResult.failed(ResultCode.ERROR_SQL_UPDATE_SERVER) : JsonResult.success(result);
    }

    @PostMapping
    @RequiresPermissions(value = "question:question:update")
    @ApiOperation(value = "更新", notes = "根据uuid更新问题")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新问题")
    public JsonResult<QuestionProblemEntity> update(@RequestParam("uuid") UUID uuid, @RequestBody @Valid QuestionProblemValidation questionProblemValidation) {
        QuestionProblemEntity result = this.problemService.update(uuid, questionProblemValidation);
        return null == result ? JsonResult.failed(ResultCode.ERROR_SQL_UPDATE_SERVER) : JsonResult.success(result);
    }

    @DeleteMapping
    @RequiresPermissions(value = "question:question:delete")
    @ApiOperation(value = "删除", notes = "根据uuid删除问题")
    @SystemJurisdiction(resourceType = "button", permissionName = "删除问题")
    public JsonResult<Object> delete(@RequestBody HashMap<String, UUID> param) {
        return this.problemService.delete(param.get("uuid")) ? JsonResult.success() : JsonResult.failed(ResultCode.ERROR_DELETE);
    }

    @DeleteMapping(value = "batch")
    @RequiresPermissions(value = "question:question:delete:batch")
    @ApiOperation(value = "批量删除", notes = "根据uuid列表批量删除")
    @SystemJurisdiction(resourceType = "button", permissionName = "批量删除问题")
    public JsonResult<HashMap<String, Integer>> deletes(@RequestBody HashMap<String, Set<UUID>> param) {
        int result = this.problemService.delete(param.get("uuids"));
        return JsonResult.success(new HashMap<String, Integer>(1) {{
            put("successNum", result);
        }});
    }
}
